package com.ruoyi.category.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.category.domain.CategoryWithProductsDTO;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.goods.domain.Goods;
import com.ruoyi.goods.domain.GoodsInfoDTO;
import com.ruoyi.goods.service.impl.GoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.category.mapper.GoodsCategoryMapper;
import com.ruoyi.category.domain.GoodsCategory;
import com.ruoyi.category.service.IGoodsCategoryService;

/**
 * 商品分类信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-01
 */
@Service
public class GoodsCategoryServiceImpl implements IGoodsCategoryService 
{
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Autowired
    private GoodsServiceImpl goodsService;

    /**
     * 查询商品分类信息
     * 
     * @param categoryId 商品分类信息主键
     * @return 商品分类信息
     */
    @Override
    public GoodsCategory selectGoodsCategoryByCategoryId(Long categoryId)
    {
        return goodsCategoryMapper.selectGoodsCategoryByCategoryId(categoryId);
    }

    /**
     * 查询商品分类信息列表
     * 
     * @param goodsCategory 商品分类信息
     * @return 商品分类信息
     */
    @Override
    public List<GoodsCategory> selectGoodsCategoryList(GoodsCategory goodsCategory)
    {
        return goodsCategoryMapper.selectGoodsCategoryList(goodsCategory);
    }

    /**
     * 新增商品分类信息
     * 
     * @param goodsCategory 商品分类信息
     * @return 结果
     */
    @Override
    public int insertGoodsCategory(GoodsCategory goodsCategory)
    {
        goodsCategory.setCreateBy(SecurityUtils.getUsername());
        goodsCategory.setCreateTime(DateUtils.getNowDate());
        return goodsCategoryMapper.insertGoodsCategory(goodsCategory);
    }

    /**
     * 修改商品分类信息
     * 
     * @param goodsCategory 商品分类信息
     * @return 结果
     */
    @Override
    public int updateGoodsCategory(GoodsCategory goodsCategory)
    {
        goodsCategory.setUpdateBy(SecurityUtils.getUsername());
        goodsCategory.setUpdateTime(DateUtils.getNowDate());
        return goodsCategoryMapper.updateGoodsCategory(goodsCategory);
    }

    /**
     * 批量删除商品分类信息
     * 
     * @param categoryIds 需要删除的商品分类信息主键
     * @return 结果
     */
    @Override
    public int deleteGoodsCategoryByCategoryIds(Long[] categoryIds)
    {
        return goodsCategoryMapper.deleteGoodsCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除商品分类信息信息
     * 
     * @param categoryId 商品分类信息主键
     * @return 结果
     */
    @Override
    public int deleteGoodsCategoryByCategoryId(Long categoryId)
    {
        return goodsCategoryMapper.deleteGoodsCategoryByCategoryId(categoryId);
    }

    /**
     * 获取商品分类信息树列表
     * @return
     */
    @Override
    public List<GoodsCategory> selectCategoryList() {
        return goodsCategoryMapper.selectCategoryList();
    }

    /**
     * 获取分类信息及对应的商品信息
     * @return
     */
    @Override
    public List<CategoryWithProductsDTO> getCategoryWithProducts() {
        // 获取所有分类
        List<GoodsCategory> categories = goodsCategoryMapper.selectCategoryList();

        List<CategoryWithProductsDTO> result = new ArrayList<>();

        for (GoodsCategory category : categories) {
            // 创建分类数据
            CategoryWithProductsDTO categoryDTO = new CategoryWithProductsDTO();
            categoryDTO.setId(category.getCategoryId());
            categoryDTO.setName(category.getCategoryName());

            // 查询该分类下的商品
            Goods goods = new Goods();
            goods.setCategoryId(category.getCategoryId());
            List<Goods> goodsList = goodsService.selectGoodsList(goods);

            // 转换商品数据
            List<GoodsInfoDTO> products = new ArrayList<>();
            for (Goods good : goodsList) {
                GoodsInfoDTO product = new GoodsInfoDTO();
                product.setId(good.getGoodsId());
                product.setName(good.getGoodsName());
                product.setPrice(good.getSalePrice());
                product.setOriginalPrice(good.getPrice());
                product.setImage(good.getImgUrl());


                products.add(product);
            }

            categoryDTO.setProducts(products);
            result.add(categoryDTO);
        }

        return result;
    }

    /**
     * 获取分类信息及对应的商品信息（根据商家ID筛选）
     * @param merchantId 商家ID，如果为null则查询所有商家的商品
     * @return
     */
    @Override
    public List<CategoryWithProductsDTO> getCategoryWithProducts(Long merchantId) {
        // 获取所有分类
        List<GoodsCategory> categories = goodsCategoryMapper.selectCategoryList();

        List<CategoryWithProductsDTO> result = new ArrayList<>();

        for (GoodsCategory category : categories) {
            // 创建分类数据
            CategoryWithProductsDTO categoryDTO = new CategoryWithProductsDTO();
            categoryDTO.setId(category.getCategoryId());
            categoryDTO.setName(category.getCategoryName());

            // 查询该分类下的商品
            Goods goods = new Goods();
            goods.setCategoryId(category.getCategoryId());
            // 如果merchantId不为null，则设置商家ID进行筛选
            if (merchantId != null) {
                goods.setMerchantId(merchantId);
            }
            List<Goods> goodsList = goodsService.selectGoodsList(goods);

            // 转换商品数据
            List<GoodsInfoDTO> products = new ArrayList<>();
            for (Goods good : goodsList) {
                GoodsInfoDTO product = new GoodsInfoDTO();
                product.setId(good.getGoodsId());
                product.setName(good.getGoodsName());
                product.setPrice(good.getSalePrice());
                product.setOriginalPrice(good.getPrice());
                product.setImage(good.getImgUrl());

                products.add(product);
            }

            categoryDTO.setProducts(products);
            result.add(categoryDTO);
        }

        return result;
    }


}
